package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.dao.UserAddressMapper;
import mobi.hami.huapin.daobase.CrudMapper;
import mobi.hami.huapin.dto.UserNoteDTO;
import mobi.hami.huapin.model.UserAddress;
import mobi.hami.huapin.service.UserAddressService;
import mobi.hami.huapin.service.UserNoteLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Create by 小峰 on 2018/8/23.
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private UserNoteLogService userNoteLogService;


    @Override
    public List<UserAddress> getUserAddressByPoiId(String poiId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setPoi_id(poiId);
        return userAddressMapper.select(userAddress);
    }

    @Override
    public int save(UserAddress userAddress) {
        if (userAddress.getId() > 0) {
            return userAddress.getId();
        } else {
            userAddress.setIs_valid(1);
            userAddressMapper.insertSelective(userAddress);
            return userAddress.getId();
        }
    }

    @Override
    public UserAddress selectOnebyId(Integer id) {
        return userAddressMapper.selectByPrimaryKey(id);
    }

    /**
     * 纸条地址信息
     *
     * @param poiId
     * @return
     */
    public List<Map<String, Object>> getNoteAddressByPoiId(String poiId) {
        UserAddress userAddress = new UserAddress();
        userAddress.setPoi_id(poiId);
        List<UserAddress> userAddressList = userAddressMapper.select(userAddress);

        List<Map<String, Object>> mapList = new ArrayList<>();


        if (userAddressList == null || userAddressList.size() <= 0) {
            return mapList;
        }

        userAddressList.forEach(m -> {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("addressId", m.getId());
            resultMap.put("addressName", m.getDetail_address());
            List<UserNoteDTO> userNoteDTOS = userNoteLogService.getUserNoteLogSevenDayCountBySpaceId(m.getId());
            int qipaoCount = 0;
            if (userNoteDTOS != null && userNoteDTOS.size() > 0) {
                qipaoCount = userNoteDTOS.stream().mapToInt(u -> u.getNote_count()).sum();
            }
            resultMap.put("userNoteList",userNoteDTOS);
            resultMap.put("noteSum", qipaoCount);
            resultMap.put("NoteList", userNoteLogService.getLastNewFiveNoteLogBySpaceId(m.getId()));
            mapList.add(resultMap);
        });
        Collections.sort(mapList,new Comparator<Map<String,Object>>(){
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
               return Integer.parseInt(o1.get("noteSum").toString())  - Integer.parseInt(o2.get("noteSum").toString());
            }
        }.reversed());
        return mapList;
    }


}
